shape()函数的用法 | 您所在的位置:网站首页 › shapeof you什么意思 › shape()函数的用法 |
Numpy中shape()函数的意义及相关用法
shape函数的功能是读取矩阵或数组的长度 **通常有三种使用方法:shape[0],shape[1],shape shape[0] :读取行数 shape[1]:读取列数 shape:行列数组成元组直接输出**** 一、当数组或矩阵是一维时** 1、只能使用shape[0],返回的是数组或矩阵中元素的个数 a=np.array([2,3,4]) print(a.shape[0])输出结果: 32、 如果使用shape[1] a=np.array([2,3,4]) print(a.shape[1])输出报错: print(b.shape[1]) IndexError: tuple index out of range 二、当数组或矩阵是二维时这里需要注意在写矩阵或数组的外面是一个()和一个[] 1、shape[0] :读取行数 import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(a.shape[0])输出结果: 42、shape[1]:读取列数 import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(a.shape[1])输出结果: 33、shape:行列数组成元组直接输出 import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(a.shape)输出结果: (4, 3) 三、当数组或矩阵是三维时这里需要注意在写矩阵或数组的外面是一个()和两个个[] import numpy as np a=np.array([[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]]) print(a.shape)输出结果: (1,4,3) |
CopyRight 2018-2019 实验室设备网 版权所有 |